package com.techjava.springboothystrixdemo.hystrix.services.impl;

import com.netflix.hystrix.*;
import org.springframework.stereotype.Service;

/**
* @desc   该类模拟订单Command
* @author kanggw
* @datetime 2021/9/3,16:28
*/
@Service
public class OrderCommandNoDelay extends HystrixCommand<String> {

    public OrderCommandNoDelay() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("demo"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("orderCommandNoDelay"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("orderThreadPool"))
                        .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                                .withCoreSize(2)
                                .withMaxQueueSize(3)
                                .withQueueSizeRejectionThreshold(3))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                                .withExecutionTimeoutInMilliseconds(15000)
                                        .withCircuitBreakerRequestVolumeThreshold(7)
                                        .withCircuitBreakerErrorThresholdPercentage(50)
                                        .withCircuitBreakerSleepWindowInMilliseconds(5000)));
    }

    @Override
    protected String run() throws Exception {
        System.out.println(Thread.currentThread().getName() +":开始查询订单");
        return OrderService.queryByOrderId(false);
    }

    @Override
    protected String getFallback() {
        System.out.println(Thread.currentThread().getName()+"： invoke fallBack");
        if (this.circuitBreaker.isOpen()) {
            System.out.println("熔断器处于开启状态");
        } else {
            System.out.println("熔断器处于关闭状态");
        }
        return "====订单查询失败，我是降级方法====";
    }
}
